﻿package com.shootingGame.weaponsClasses.ordinance
{
	import com.shootingGame.GamePlayManager;
	import com.shootingGame.physics.collisionReactionClasses.OrdinanceCollisionEffects;
	import com.shootingGame.physics.collisionReactionClasses.base.CollisionEffects;
	import com.shootingGame.renderableClasses.GameObject;
	import com.shootingGame.renderableClasses.MobileObject;
	import com.shootingGame.weaponsClasses.WeaponInfo;
	import com.shootingGame.weaponsClasses.launchProfiles.LaunchProfile;
	
	import flash.display.MovieClip;
	import flash.geom.Point;

	public class Ordinance extends MobileObject
	{
		private var _liveTime : int;
		private var _liveTimeRemaining : int;
		public var defaultspeed : Number;
		public var owner : GameObject;
		protected var _launchProfile:String;
		public var source : Point;
		public var target : Point;
		public var weaponInfo : WeaponInfo;
		
		
		
		public function Ordinance(model:MovieClip, owner : GameObject, weaponInfo : WeaponInfo, sourceX:Number, sourceY:Number, targetX:Number, targetY:Number, collisionEffects:CollisionEffects = null, travelDirection:Number = 0, defaultSpeed : Number = 1, travelSpeedModifier:Number = 1, liveTime : int = 5000, max_Speed:Number = 0, acceleration:Number = 0, deceleration:Number = 0)
		{
			super(model, sourceX, sourceY, max_Speed, acceleration, deceleration);
			isToBeRendered = true;
			isToBeUpdated = true;
			isHitDetectionEnabled = true;
			this.owner = owner;
			_launchProfile = weaponInfo.launchprofile;
			source = new Point(sourceX, sourceY);
			target = new Point(targetX, targetY);
			facing = travelDirection;
			this.defaultspeed = defaultspeed;
			//speed = defaultspeed * travelSpeedModifier;
			speed = weaponInfo.ordinanceSpeed;
			velocityDirection = travelDirection;
			_liveTime = liveTime;
			_liveTimeRemaining = _liveTime;
			this.weaponInfo = weaponInfo;
			GamePlayManager.instance.ordinanceList.push(this);
			if(model.collideAnimation)
				model.collideAnimation.visible = false;
			if(collisionEffects)
				this.collisionEffects = collisionEffects;
			else
				this.collisionEffects = new OrdinanceCollisionEffects(this);
			//Math.cos(travelDirection/180*Math.PI) * travelSpeed, Math.sin(travelDirection/180*Math.PI) * travelSpeed
		}
		
		override public function update():void{
			if(isExpired)
				return;
			liveTimeRemaining -= 1;
//			if(memoryX >= GamePlayManager.stageWidth || memoryX <= 0 || memoryY >= GamePlayManager.stageHeight || memoryY <= 0){
//				isExpired = true;
//			}
			if(liveTimeRemaining <= 0){
				isExpired = true;
			}
			if(!isExpired)
				super.update();
		}
		
		override protected function updateThrust():void{
			LaunchProfile.updateTrajectory(_launchProfile, this, source, target);
		}

		override public function addObject_ToUpdateList():void
		{
			if(GamePlayManager.instance.ordinanceList.indexOf(this) == -1)
				GamePlayManager.instance.ordinanceList.push(this);
		}
		
		override public function removeObject_FromUpdateList():void{
			var ordinanceList : Array = GamePlayManager.instance.ordinanceList;
			ordinanceList.splice(ordinanceList.indexOf(this),1);			
		}
		
		public function get liveTimeRemaining() : int{
			return _liveTimeRemaining;
		}
		
		public function set liveTimeRemaining(value : int) {
			_liveTimeRemaining = value;
			if(_liveTimeRemaining < 0)
				_liveTimeRemaining = 0;
		}
		
		override public function set isExpired(value:Boolean):void{
			super.isExpired = value;
			if(isExpired){
				if(model.collideAnimation){
//					model.gotoAndStop("collideAnimation");
					model.collideAnimation.x = memoryX;
					model.collideAnimation.y = memoryY;
					GamePlayManager.instance.effectsHolder.addChild(model.collideAnimation);
					model.collideAnimation.visible = true;
					model.collideAnimation.gotoAndPlay(1);
				}
				cache();
			}
		}	
		
		
/**
* When overridden in a derieved class, returns a new or cached instance of the ordinance.
*
* @return Returns a new or cached instance of the ordinance.
*/
		virtual public function clone() : Ordinance {
			return null;
		}

 /**
 * When overridden in a derieved class, this instance is cached for later use.
 */		
		virtual protected function cache() : void {
			return;
		}

/**
* Resets the liveTimeRemaining, and any other counters or states to that of a newly created Ordinance object.
*/		
		virtual public function reset() : void {
			liveTimeRemaining = _liveTime;
		}
	}
}
